/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.common.client.model;

import java.util.ArrayList;
import java.util.Date;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

public class ProductModel extends AbstractModel
{	
	protected int id;
	protected int categoryId;
	protected int manufacturerId;
	protected double discountPrice;
	protected String image = null;
	protected String[] imageSize = null;
	protected String name; 
	protected String model;
	protected double price;
	protected double specialPrice;

	protected boolean hasAttributes;
	protected boolean hasReviews;
	protected int numberOfReviews;
	protected double reviewsAverage;

	protected int inventoryQuantity;
	protected int quantityBlocks;
	protected Date dateAdded;
	protected double weight;
	protected boolean status;
	protected boolean virtual;
	
	protected double imageHeightMax;
	protected double imageWidthMax;
	
	protected double productImageHeightMax;
	protected double productImageWidthMax;
	
	public ProductModel()
	{
		update();
	}

	public ProductModel(int categoryId, int id, String name)
	{
		update(categoryId, id, name);
	}

	public ProductModel(JSONObject jsonObject, double imageWidthMaxDefault, double imageHeightMaxDefault)
	{
		update();

		imageWidthMax = convertToDouble(jsonObject, "image_width_max");
		imageHeightMax = convertToDouble(jsonObject, "image_height_max");
		productImageWidthMax = imageWidthMax;
		productImageHeightMax = imageHeightMax;
		if (imageWidthMax < 1.0) imageWidthMax = imageWidthMaxDefault;
		if (imageHeightMax < 1.0) imageHeightMax = imageHeightMaxDefault;
		
		id = convertToInteger(jsonObject, "id");
		categoryId = convertToInteger(jsonObject, "categories_id");
		manufacturerId = convertToInteger(jsonObject, "manufacturer_id");

		image = convertToString(jsonObject, "image");
		double imageWidth = convertToDouble(jsonObject, "image_width");
		double imageHeight = convertToDouble(jsonObject, "image_height");
		imageSize = adjustedImageSize(imageWidth, imageHeight, imageWidthMax, imageHeightMax);

		name = convertToString(jsonObject, "name");
		model = convertToString(jsonObject, "model");
		price = convertToDouble(jsonObject, "price");
		discountPrice = convertToDouble(jsonObject, "discount_price");
		specialPrice = convertToDouble(jsonObject, "special_price");
		
		hasAttributes = convertToBoolean(jsonObject, "products_attributes");
		hasReviews = convertToBoolean(jsonObject, "products_reviews");
		numberOfReviews = convertToInteger(jsonObject, "count_reviews");
		reviewsAverage = convertToDouble(jsonObject, "reviews_average");

		inventoryQuantity = convertToInteger(jsonObject, "inventory_qty");
		quantityBlocks = convertToInteger(jsonObject, "qty_blocks");
		dateAdded = convertToDate(jsonObject, "date_added");
		weight = convertToDouble(jsonObject, "weight");
		status = convertToBoolean(jsonObject, "status");
		virtual = convertToBoolean(jsonObject, "virtual");
	}

	public void update()
	{
		categoryId = 0;
		id = 0;
		discountPrice = 0;
		image = "";
		imageSize = new String[2];
		imageSize[0] = "1";
		imageSize[1] = "1";
		name = ""; 
		model = "";
		price = 0;
		specialPrice = 0;

		hasAttributes = false;
		hasReviews = false;
		numberOfReviews = 0;
		reviewsAverage = 5;
		inventoryQuantity = 0;
		quantityBlocks = 1;
		dateAdded = null;
		weight = 0;
		status = false;
		virtual = false;
	}

	public void update(int categoryId, int id, String name)
	{
		update();
		this.categoryId = categoryId;
		this.id = id;
		this.name = name; 
	}

	public void update(ProductModel model)
	{
		categoryId = model.getCategoryId();
		id = model.getId();
		discountPrice = model.getDiscountPrice();
		image = model.getImage();
		imageSize = model.getImageSize();
		name = model.getName(); 
		this.model = model.getModel();
		price = model.getPrice();
		specialPrice = model.getSpecialPrice();

		hasAttributes = model.isHasAttributes();
		hasReviews = model.isHasReviews();
		numberOfReviews = model.getNumberOfReviews();
		reviewsAverage = model.getReviewsAverage();
		
		inventoryQuantity = model.getInventoryQuantity();
		quantityBlocks = model.getQuantityBlocks();
		dateAdded = model.getDateAdded();
		weight = model.getWeight();
		status = model.isStatus();
		virtual = model.isVirtual();
	}

	public String getImage() {
		return image;
	}

	public String[] getImageSize() {
		return imageSize;
	}

	public int getCategoryId() {
		return categoryId;
	}

	public int getId() {
		return id;
	}

	public int getManufacturerId() {
		return manufacturerId;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public String getModel() {
		return model;
	}

	public double getPrice() {
		return price;
	}

	public double getSpecialPrice() {
		return specialPrice;
	}

	public double getDiscountPrice() {
		return discountPrice;
	}

	public void setDiscountPrice(double discountPrice) {
		this.discountPrice = discountPrice;
	}

	public boolean isHasReviews() {
		return hasReviews;
	}
	
	public int getNumberOfReviews() {
		return numberOfReviews;
	}
	
	public double getReviewsAverage() {
		return reviewsAverage;
	}

	public boolean isHasAttributes() {
		return hasAttributes;
	}

	public void setHasAttributes(boolean hasAttributes) {
		this.hasAttributes = hasAttributes;
	}

	public int getInventoryQuantity() {
		return inventoryQuantity;
	}

	public int getQuantityBlocks() {
		int qtyBlocks = 1;
		if (quantityBlocks > 1)qtyBlocks = quantityBlocks;
		return qtyBlocks;
	}

	public Date getDateAdded() {
		return dateAdded;
	}

	public double getWeight() {
		return weight;
	}

	public boolean isStatus() {
		return status;
	}

	public boolean isVirtual() {
		return virtual;
	}

	public static ArrayList<ProductModel> buildProductModels(JSONArray jsonArray, double imageWidthMax, double imageHeightMax)
	{
		ArrayList<ProductModel> productModels = new ArrayList<ProductModel>();
		int sz = jsonArray.size();		
		for (int i = 0; i < sz; i++)
		{
			JSONValue jsonValue = jsonArray.get(i);
			JSONObject jsonObject;
			if ((jsonObject = jsonValue.isObject()) != null)
			{
				ProductModel productModel = new ProductModel(jsonObject, imageWidthMax, imageHeightMax);
				productModels.add(productModel);
			}
		}
		return productModels;
	}

	public double getProductImageHeightMax() {
		return productImageHeightMax;
	}

	public double getProductImageWidthMax() {
		return productImageWidthMax;
	}
}
